#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "beep.h"
#include "pwm.h"
#include "bump.h"
#include "strage.h"
#include "time.h"
#include "hx711.h"
#include "esp8266.h"
#include "onenet.h"
#include "usart.h"
#include "dht11.h"
#include "stdio.h"
#include <string.h>
#define ESP8266_ONENET_INFO		"AT+CIPSTART=\"TCP\",\"mqtts.heclouds.com\",1883\r\n"
DHT11_Data_TypeDef dht11_data;
void dht11_show()
{
	if(Read_DHT11(&dht11_data))
    {

      OLED_ShowString(3, 1, "temp:");
		 OLED_ShowString(4, 1, "humi:");
		 OLED_ShowString(3, 9, ".");
		 OLED_ShowString(4, 9, ".");
      OLED_ShowNum(3, 7, dht11_data.temp_int, 2);
	   OLED_ShowNum(3, 10, dht11_data.temp_deci, 2);
		 OLED_ShowNum(4, 7, dht11_data.humi_int, 2);
	   OLED_ShowNum(4, 10, dht11_data.humi_deci, 2);


       OLED_ClearPartial(7, 3, 2); 
		  OLED_ClearPartial(9, 3, 2); 
		 OLED_ClearPartial(10, 3, 2); 
		 OLED_ClearPartial(7, 4, 2); 
		  OLED_ClearPartial(9, 4, 2); 
		 OLED_ClearPartial(10, 4, 2); 
		 Delay_ms(500);
		 OLED_ShowString(3, 9, ".");
		 OLED_ShowString(4, 9, ".");
		 OLED_ShowNum(4, 7, dht11_data.humi_int, 2);
		 OLED_ShowNum(4, 10, dht11_data.humi_deci,2);
		 OLED_ShowNum(3, 7, dht11_data.temp_int, 2);
		 OLED_ShowNum(3, 10, dht11_data.temp_deci, 2);
		 Delay_ms(1500);
    }
}
void weight_test()
{
		
		Get_Weight1();
}
int main(void)
{
	unsigned short timeCount = 0;	//发送间隔变量
	unsigned char *dataPtr = NULL;
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2：2位抢占优先级，2位响应优先级 
	PWM_Init();
	DHT11_GPIO_Config();
	Init_HX711pin1();
	Servo_transform(0);
	Usart1_Init(115200);
	Usart2_Init(115200);							//串口2，驱动ESP8266用
	ESP8266_Init();					//初始化ESP8266
	UsartPrintf(USART_DEBUG, "Connect MQTTs Server...\r\n");
	while(ESP8266_SendCmd(ESP8266_ONENET_INFO, "CONNECT"))
		Delay_ms(500);
	UsartPrintf(USART_DEBUG, "Connect MQTT Server Success\r\n");
//	
	while(OneNet_DevLink())			//接入OneNET
		Delay_ms(500);
	OneNET_Subscribe();
		

	
	while(1)
	{
		
		Read_DHT11(&dht11_data);
	
		
		weight_test();
		UsartPrintf(USART_DEBUG, "weight:%d\r\n",Weight_Shiwu1);
		UsartPrintf(USART_DEBUG, "temp:%d\r\n",dht11_data.temp_int);
		UsartPrintf(USART_DEBUG, "humi:%d\r\n",dht11_data.humi_int);
			if(++timeCount >= 100)									//发送间隔5s
		{
			Read_DHT11(&dht11_data);
			weight_test();
			UsartPrintf(USART_DEBUG, "OneNet_SendData\r\n");
			OneNet_SendData();									//发送数据
			
			timeCount = 0;
			ESP8266_Clear();
		}
		dataPtr = ESP8266_GetIPD(0);
		if(dataPtr != NULL)
			OneNet_RevPro(dataPtr);
		
		Delay_ms(10);
	}
}
//int main(void) {
////	unsigned short timeCount = 0;	//发送间隔变量
////	unsigned char *dataPtr = NULL;	
//    /* 模块初始化 */
//	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2：2位抢占优先级，2位响应优先级 	
//    OLED_Init();            // OLED初始化
//	DHT11_GPIO_Config();
////    beep_init();
////    bump_init();
//   Usart2_Init(115200);	
////	HX711_Init();
////	Get_Weight();
////	Usart2_Init(115200);     //串口2初始化为115200
//	
////	ESP8266_Init();
////    mqtt_init();           // 初始化MQTT客户端
////	OLED_ShowChar(1, 1, 'B'); 
//// OLED_ShowString(2, 1, "weight:");
////     PWM_Init();
////    strage_init();
////    time4_init(199, 7199);
////	TIM2_Int_Init(199, 7199);
////    ESP8266_Init();         // 初始化ESP8266模块
////		TIM2_Int_Init(10000 - 1, 7200 - 1);
//    printf("ni hao\n");
//	 OLED_Clear();

////	while(1)
////	{	    	 
////		Get_Weight(); 
//////		if(water_stage() == 0)
//////		{
//////			TIM_Cmd(TIM2, ENABLE);  //开启定时器	
//////		}else
//////		{
//////		
//////			TIM_Cmd(TIM2, DISABLE);  //开启定时器	
//////			
//////		}
////	
////	}
////}
//while(1){
////dht11_show();
//}//接入OneNET
////	Delay_ms(500);	
//////	OLED_ShowChar(1, 2, 'C');
////	while(1)
////	{	    	    
////		

////		
////		if(++timeCount >= 50)									//发送间隔5s
////				{		
//////					data_len=MqttOnenet_Savedata(send_jason,temperature, humidity);
////					OneNet_SendData();									//发送数据
////					
////					timeCount = 0;
////					ESP8266_Clear();
////				}				
////			  dataPtr = ESP8266_GetIPD(0);
////			  if(dataPtr != NULL)
////				OneNet_RevPro(dataPtr);

////	}


//}
